﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace SportTracksSwimSetPlugin.Common
{
    static class Log
    {

        // 
        // log levels to be used
        // 
        public enum LogLevel
        {
            DEBUG = 0,
            INFO = 1,
            WARN = 2,
            ERROR = 3
        };


        //
        // Summary:
        //     Creates a log entry into the logvile
        //
        // Parameters:
        //   logline:
        //     The text to be written
        //
        //   logLevel:
        //     The LogLevel for the log being written
        //
        public static void WriteToLogfile(string logline, LogLevel logLevel)
        {
            String tsString = DateTime.Now.TimeOfDay.ToString();

            // we only want to actually write logs that are at least the level that are
            // turned on. So, wo don't want to print DEBUG logs when the system level is INFO
            // We do want to print WARN logs if system level is DEBUG
            if (logLevel <= sysLogLevel)
            {
                try
                {
                    StreamWriter writer = new StreamWriter(logFileName, _append);
                    writer.WriteLine(tsString + ":" +logLevel.ToString() + " " +logline);
                    writer.Close();
                    // from now on, append to the file
                    _append = true;
                }
                catch { }
            }
        }


        // currently this isn't exposed in the settings page, so
        // let's just always get debug level for now
        private static LogLevel sysLogLevel = LogLevel.DEBUG;

        private static bool _append = false;
        private static string logFileName = Path.GetTempPath() + "SwimSetPlugin.log";
    }
}
